//
//  MHAppCollectListViewController.m
//  MHO
//
//  Created by Hien NV on 7/11/14.
//  Copyright (c) 2014 maihoang. All rights reserved.
//

#import "MHAppCollectListViewController.h"
#import "Lib.h"
#import "AppCollectCell.h"
#import "Application.h"
#import "MHAppDelegate.h"
#import "Collection.h"
#import "MHGameDetailViewController.h"

@interface MHAppCollectListViewController ()

@end

@implementation MHAppCollectListViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.tableView registerNib:[UINib nibWithNibName:@"AppCollectCell" bundle:nil] forCellReuseIdentifier:@"AppCollectCell"];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setCollection:(Collection *)collection {
    _collection = collection;
    
    NSString* url = [NSString stringWithFormat:@"http://mobile.mho.vn/services/chude?gameid=0&chudeid=%@&pageIndex=%@&pageSize=%@&key=%@", _collection.colletionId, @"%d", @"%d", kServerKey];
    NSLog(@"url: %@", url);
    self.listUrl = url;
}

- (void)setFavorite:(BOOL)isFavorite {
    _isFavorite = isFavorite;
    
    if (_isFavorite) {
        NSString* url = [NSString stringWithFormat:@"http://mobile.mho.vn/services/LikeGame?GameId=0&IdSystem=2&User=%@&pageIndex=%@&pageSize=%@&key=%@", [Lib stringForKey:@"Email"], @"%d", @"%d", kServerKey];
        self.listUrl = url;
        [[self.navibarView leftButton] setImage:[UIImage imageNamed:kFileBtnHuman] forState:UIControlStateNormal];
    }
}

- (void)leftButtonPressed:(id)sender {
    if (_isFavorite) {
        [[MHAppDelegate sharedApp].viewController showLeftPanelAnimated:YES];
    }
    else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

#pragma mark - TableViewSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.data.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString* CellIndentifier = @"AppCollectCell";
        
    AppCollectCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIndentifier];
    if (!cell) {
        cell = [AppCollectCell cellFromNib];
    }
    // configure cell
    [cell setDelegate:self];
    if (indexPath.row < self.data.count) {
        Application* app = [self.data objectAtIndex:indexPath.row];
        [cell setApp:app];
        [cell setFavorite:self.isFavorite];
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 85;
}

#pragma mark - UITabelView Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    MHGameDetailViewController* vc = [[MHGameDetailViewController alloc] initWithNibName:@"MHGameDetailViewController" bundle:nil];
    Application* app = [self.data objectAtIndex:indexPath.row];
    vc.appId = app.appId.integerValue;
    vc.app = app;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - AppCollectDelegate
- (void)deleteGameId:(NSString *)appId {
    [UIAlertView showWithTitle:@"Thông báo" message:@"Ban có chắc muốn xoá game này khỏi bộ sưu tập không?" cancelButtonTitle:@"Có" otherButtonTitles:@[@"Không"] tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
        if(buttonIndex == 0) {
            NSString* url = [NSString stringWithFormat:@"http://mobile.mho.vn/services/ChuDe?Type=2&GameId=%@&ChuDeId=%@&key=%@", appId, self.collection.colletionId, kServerKey];
            if (self.isFavorite) {
                url = [NSString stringWithFormat:@"http://mobile.mho.vn/services/LikeGame?Type=2&IdSystem=2&GameId=%@&User=%@&key=%@", appId, [Lib stringForKey:@"Email"], kServerKey];
            }
            NSLog(@"delete game url: %@", url);
            
            [Lib showLoadingView];
            
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                NSString* ret = [ServiceLib sendGetRequest:url useCache:NO];
                
                if (ret) {
                    NSLog(@"delete game from collection: %@", ret);
                }
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    [Lib removeLoadingView];
                    [Lib showMessage:@"Đã xoá game khỏi danh sách thành công!" title:@"Thông báo"];
                    [self refresh];
                });
            });
        }
    }];
}
@end
